package com.cheyun.netsalev3.utils

import android.content.Context
import android.content.SharedPreferences
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.bean.CustomListParam
import com.cheyun.netsalev3.bean.Notice
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.dm.DmLogin
import com.cheyun.netsalev3.bean.login.LoginParam
import com.cheyunkeji.er.bean.UserInfoToken
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


object MySharedPreferences {

    //用于单例实例化
    @Volatile
    private var instance: SharedPreferences? = null

    private fun getInstance(): SharedPreferences {
        return instance ?: synchronized(this) {
            instance ?: createInstance().also { instance = it }
        }
    }

    private fun createInstance(): SharedPreferences {
        return MyApplication.getInstance().getSharedPreferences("appPreferences", Context.MODE_PRIVATE)
    }


    //用于单例实例化 用来保存账号密码 和是否第一次登陆等不被清除数据
    @Volatile
    private var logininstance: SharedPreferences? = null

    private fun getLogininstance(): SharedPreferences {
        return logininstance ?: synchronized(this) {
            logininstance ?: createLogininstance().also { logininstance = it }
        }
    }

    private fun createLogininstance(): SharedPreferences {
        return MyApplication.getInstance().getSharedPreferences("apploginPreferences", Context.MODE_PRIVATE)
    }

    /**
     * 保存基础数据
     */
    fun putBaseData(baseDataParam: BaseDataParam) {
        if (instance == null) {
            getInstance()
        }
        var data = Gson().toJson(baseDataParam)
        instance!!.edit().putString("baseData", data).commit()

    }

    /**
     * 得到基础数据
     */
    fun getBaseData(): BaseDataParam? {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("baseData", "")
        if (data != "") {
            return Gson().fromJson(data, BaseDataParam::class.java)
        }
        return null
    }

    fun putIsDrive(isDrive: Int) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putInt("isDrive", isDrive).commit()

    }

    /**
     * 得到基础数据
     */
    fun getIsDrive(): Int? {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getInt("isDrive", 0)
        return data
    }

    /**
     * 保存登陆数据
     */
    fun putLoginData(loginData: LoginParam) {
        if (instance == null) {
            getInstance()
        }
        var data = Gson().toJson(loginData)
        instance!!.edit().putString("logion", data).commit()
    }

    /**
     * 获取登陆数据
     */
    fun getLoginData(): LoginParam? {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("logion", "")
        if (data != "") {
            return Gson().fromJson(data, LoginParam::class.java)
        }
        return null
    }

    fun putLastModule(module: String) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putString("module", module).commit()
    }

    /**
     * 获取cauthkey
     */
    fun getLastModule(): String {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getString("module", "")
    }

    /**
     * 保存cauthkey
     */
    fun putCauthkey(cauthkey: String) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putString("cauthkey", cauthkey).commit()
    }

    /**
     * 获取cauthkey
     */
    fun getCauthkey(): String {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getString("cauthkey", "")
    }

    /**
     * 保存cauthkey
     */
    fun putDMCauthkey(cauthkey: String) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putString("dmcauthkey", cauthkey).commit()
    }
    /**
     * 获取cauthkey
     */
    fun getDMCauthkey(): String {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getString("dmcauthkey", "")
    }

    fun putDMMgr(mgr: Int) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putInt("dmmgr", mgr).commit()
    }
    fun getDMMgr(): Int {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getInt("dmmgr",0);
    }
    fun putChannel(channel: Int) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putInt("channel", channel).commit()
    }
    fun getChannel(): Int {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getInt("channel", 0)
    }
    fun putDmLoginData(data: DmLogin) {
        if (instance == null) {
            getInstance()
        }
        var d = Gson().toJson(data)
        instance!!.edit().putString("dmlogin", d).commit()
    }
    fun getDmLoginData(): DmLogin? {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("dmlogin", "")
        if (data != "") {
            return Gson().fromJson(data, DmLogin::class.java)
        }
        return null
    }


    fun putErPermissions(permission: UserInfoToken){
        if (instance == null) {
            getInstance()
        }
        if(permission == null){
            return
        }
        var data = Gson().toJson(permission)
        instance!!.edit().putString("er_permission", data).commit()
    }
    fun removeErPermissions(){
        instance!!.edit().remove("er_permission").commit()
    }
    fun getErPermissions(): UserInfoToken?{
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("er_permission", "")
        if (data != "") {
            return Gson().fromJson(data, UserInfoToken::class.java)
        }
        return null
    }

    /**
     * 设置引导页
     */
    fun setGuide(guide: Boolean) {
        if (logininstance == null) {
            getLogininstance()
        }
        logininstance!!.edit().putBoolean("guide", guide).commit()
    }

    /**
     * 获取是否需要引导页
     * true 需要引导页
     */
    fun getGuide(): Boolean {
        if (logininstance == null) {
            getLogininstance()
        }
        return logininstance!!.getBoolean("guide", true)
    }


    /**
     * 保存mobile用户名
     */
    fun putMobile(mobile: String) {
        if (logininstance == null) {
            getLogininstance()
        }
        logininstance!!.edit().putString("mobile", mobile).commit()
    }

    /**
     * 获取mobile用户名
     */
    fun getMobile(): String {
        if (logininstance == null) {
            getLogininstance()
        }
        return logininstance!!.getString("mobile", "")
    }

    /**
     * 保存password
     */
    fun putPassword(password: String) {
        if (logininstance == null) {
            getLogininstance()
        }
        logininstance!!.edit().putString("password", password).commit()
    }

    /**
     * 获取password
     */
    fun getPassword(): String {
        if (logininstance == null) {
            getLogininstance()
        }
        return logininstance!!.getString("password", "")
    }

    /**
     * 获取工作台0 个人 1 管理层 2 展厅
     */
    fun getWorkbench(): Int {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getInt("workbench", 0)
    }

    /**
     * 设置工作台
     */
    fun setWorkbench(workbench: Int) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putInt("workbench", workbench).commit()
    }

    /**
     * 设置消息通道开关数据
     */
    fun putNotice(notice: Notice) {
        if (instance == null) {
            getInstance()
        }
        var data = Gson().toJson(notice)
        instance!!.edit().putString("notice", data).commit()
    }

    /**
     * 获取消息通道开关数据
     */
    fun getNotice(): Notice? {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("notice", "")
        if (data != "") {
            return Gson().fromJson(data, Notice::class.java)
        }
        return null

    }

    /**
     * 获取自定义列表
     */
    fun getListItem(): ArrayList<CustomListParam> {
        if (instance == null) {
            getInstance()
        }
        var data = instance!!.getString("listItem", "")
        if (data != "") {
            val listType = object : TypeToken<ArrayList<CustomListParam>>() {}.type
            return Gson().fromJson(data, listType)
        }
        return ArrayList<CustomListParam>()
    }

    /**
     * 设置自定义列表
     */
    fun setListItem(list: List<CustomListParam>) {
        if (instance == null) {
            getInstance()
        }
        var data = Gson().toJson(list)
        instance!!.edit().putString("listItem", data).commit()
    }

    /**
     * 清空数据
     */
    fun clearAll() {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().clear().commit()
    }

    fun hasAuthorization():Boolean{
//        var loginData = getLoginData()
//        loginData?.channel?.level?.apply {
//            if(this>0)return true;
//        }
//        return false
        return true
    }

    /**
     * 设置同意隐私协议
     */
    fun setAgreeYs(ys: Boolean) {
        if (logininstance == null) {
            getLogininstance()
        }
        logininstance!!.edit().putBoolean("agreeys", ys).commit()
    }

    /**
     * 获取是否同意隐私协议
     * true 需要引导页
     */
    fun getAgreeYs(): Boolean {
        if (logininstance == null) {
            getLogininstance()
        }
        return logininstance!!.getBoolean("agreeys", false)
    }

    /**
     * 获取password
     */
    fun getYsChecked(): String {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getString("yscheck", "0")
    }

    fun getClueRightColumn(): String {
        if (instance == null) {
            getInstance()
        }
        return instance!!.getString("cluecolumn", "customerlevelname")
    }

    /**
     * 设置工线索列表右边选项
     */
    fun setClueRightColumn(cluecolumn: String) {
        if (instance == null) {
            getInstance()
        }
        instance!!.edit().putString("cluecolumn", cluecolumn).commit()
    }
}